import os
import threading
from langchain_core.tools import tool

from EnvironmentService import EnvironmentService

envServer = EnvironmentService()
lock = threading.Lock()


def simple_fix(test_code: str):
    if "package" in test_code:
        test_code = test_code.replace("package", "// package")
    marker = "src/test/java"
    package_name = envServer.inject_dir.split(marker, 1)[-1].strip(os.sep).replace(os.sep, ".")
    test_code = "package " + package_name + ";\n\n" + test_code
    return test_code

def compile_test(test_code: str, test_file_name: str):
    """
        Compiles a given test case.

        Args:
            test_code (str): The source code of the test to be compiled.
            test_file_name (str): The name of the file containing the test code.

        Returns:
            dict: A dictionary with the following keys:
                  - "result" (str): Indicates the outcome ("Syntax Error", "Compile Error", or "Success").
                  - "output" (str): The corresponding output or error message for the operation.
    """
    with lock:
        envServer.set_test_infor(test_file_name, test_code)
        syntax_result, syntax_output = envServer.syntax_check()
        if not syntax_result:
            return {"result": "Syntax Error", "output": syntax_output}

        envServer.test_code_inject()
        compile_result, compile_output = envServer.compile_test()
        if not compile_result:
            envServer.test_code_remove()
            return {"result": "Compile Error", "output": compile_output}
        envServer.test_code_remove()
        return {"result": "Success", "output": compile_output}

def execute_test(test_code: str, test_file_name: str):
    """
        Executes a given test case.

        Args:
            test_code (str): The source code of the test to be executed.
            test_file_name (str): The name of the file containing the test code.

        Returns:
            dict: A dictionary with the following keys:
                  - "result" (str): Indicates the outcome ("Syntax Error", "Compile Error", "Execute Error", or "Success").
                  - "output" (str): The corresponding output or error message for the operation.
    """
    with lock:
        envServer.set_test_infor(test_file_name, test_code)
        envServer.test_code_inject()
        execute_result, execute_output = envServer.execute_test()
        if not execute_result:
            envServer.test_code_remove()
            return {"result": "Execute Error", "output": execute_output}
        envServer.test_code_remove()
        return {"result": "Success", "output": execute_output}

def compile_and_execute_test(test_code: str, test_file_name: str):
    """
        Executes a sequence of operations to validate, compile, and run a given test case.

        Args:
            test_code (str): The source code of the test to be executed.
            test_file_name (str): The name of the file containing the test code.

        Returns:
            dict: A dictionary with the following keys:
                  - "result" (str): Indicates the outcome ("Syntax Error", "Compile Error", "Execute Error", or "Success").
                  - "output" (str): The corresponding output or error message for the operation.
    """
    with lock:
        envServer.set_test_infor(test_file_name, test_code)
        syntax_result, syntax_output = envServer.syntax_check()
        if not syntax_result:
            return {"result": "Syntax Error", "output": syntax_output}

        envServer.test_code_inject()
        compile_result, compile_output = envServer.compile_test()
        if not compile_result:
            envServer.test_code_remove()
            return {"result": "Compile Error", "output": compile_output}
        execute_result, execute_output = envServer.execute_test()
        if not execute_result:
            envServer.test_code_remove()
            return {"result": "Execute Error", "output": execute_output}
        envServer.test_code_remove()
        return {"result": "Success", "output": execute_output}
